package com.yanqu.road.dao.impl.activity.stronghold;

import com.alibaba.fastjson.JSON;
import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.activity.stronghold.StrongholdUploadMark;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class StrongholdUploadMarkDaoImpl extends TempDataObjectDao<StrongholdUploadMark> {
    public static String getStrongholdUploadMarkSql = "select * from t_u_activity_stronghold_upload_mark where activityId=?";
    public Map<Integer,StrongholdUploadMark> getStrongholdUploadMark(int activityId) {
        ResultSet rs = null;
        CallableStatement call = null;
        Map<Integer,StrongholdUploadMark> dataMap = new ConcurrentHashMap<>();
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareCall(getStrongholdUploadMarkSql);
            int i = 1;
            call.setInt(i++,activityId);
            rs = call.executeQuery();
            while (rs.next()) {
                StrongholdUploadMark strongholdUploadMark = parse(rs);
                dataMap.put(strongholdUploadMark.getRound(), strongholdUploadMark);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);} finally {
            getDb().closeConnection(call, rs);
        }
        return dataMap;
    }

    public static String updateStrongholdUploadMarkSql = "update t_u_activity_stronghold_upload_mark set isUpload=?,qualifyUnionList=?,lastUploadTime=? where activityId=? and round=?";
    @Override
    protected boolean updateData(StrongholdUploadMark strongholdUploadMark) {
        strongholdUploadMark.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.BOOLEAN, strongholdUploadMark.isUpload()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(strongholdUploadMark.getQualifyUnionList())));
        para.put(i++, new DbParameter(Types.BIGINT, strongholdUploadMark.getLastUploadTime()));
        para.put(i++, new DbParameter(Types.INTEGER, strongholdUploadMark.getActivityId()));
        para.put(i++, new DbParameter(Types.INTEGER, strongholdUploadMark.getRound()));
        if (getDb().executeNoneQuery(updateStrongholdUploadMarkSql, para) > 0) {
            return true;
        }
        return false;
    }

    public static String insertStrongholdUploadMarkSql = "insert into t_u_activity_stronghold_upload_mark(activityId,round,isUpload,qualifyUnionList,lastUploadTime) values (?,?,?,?,?)";
    @Override
    protected boolean addData(StrongholdUploadMark strongholdUploadMark) {
        strongholdUploadMark.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, strongholdUploadMark.getActivityId()));
        para.put(i++, new DbParameter(Types.INTEGER, strongholdUploadMark.getRound()));
        para.put(i++, new DbParameter(Types.BOOLEAN, strongholdUploadMark.isUpload()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(strongholdUploadMark.getQualifyUnionList())));
        para.put(i++, new DbParameter(Types.BIGINT, strongholdUploadMark.getLastUploadTime()));
        if (getDb().executeNoneQuery(insertStrongholdUploadMarkSql, para) > 0) {
            return true;
        }
        return false;

    }

    @Override
    protected StrongholdUploadMark parseData(ResultSet rs) throws SQLException {StrongholdUploadMark strongholdUploadMark = new StrongholdUploadMark();
        strongholdUploadMark.setActivityId(rs.getInt("activityId"));
        strongholdUploadMark.setRound(rs.getInt("round"));
        strongholdUploadMark.setUpload(rs.getBoolean("isUpload"));
        List<String> qualifyUnionList = JSON.parseArray(rs.getString("qualifyUnionList"), String.class);
        strongholdUploadMark.setQualifyUnionList(new HashSet<>(qualifyUnionList));
        strongholdUploadMark.setLastUploadTime(rs.getLong("lastUploadTime"));
        return strongholdUploadMark;
    }
}
